package com.quanyan.user.web.admin;

import com.quanyan.api.APIResponse;
import com.quanyan.common.Plugin.PageObj;
import com.quanyan.common.utils.StringUtil;
import com.quanyan.user.model.db.TbAdminGroup;
import com.quanyan.user.model.db.TbUser;
import com.quanyan.user.model.enums.ComplaintType;
import com.quanyan.user.model.vo.request.*;
import com.quanyan.user.model.vo.request.admin.*;
import com.quanyan.user.model.vo.response.admin.RespAppVersionInfo;
import com.quanyan.user.service.UserInformationService;
import com.quanyan.user.service.admin.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * 个人信息管理后台管理接口
 * Created by hua on 16/8/4.
 */
@RestController
@RequestMapping(value = "/admin/api/user/information")
public class AdminUserInformationController {
    private static final Logger logger = LoggerFactory.getLogger(AdminUserInformationController.class);
    @Autowired
    private UserVerifyService userVerifyService;
    @Autowired
    private UserService userService;
    @Autowired
    private UserInterestService userInterestService;
    @Autowired
    private UserComplainantService userComplainantService;
    @Autowired
    private AppVersionInfoService appVersionInfoService;
    @Autowired
    private UserAccountBindService userAccountBindService;
    @Autowired
    private UserInformationService  userInformationService;
    /**
     * 获取用户认证列表
     *
     * @param userVerifyQuery
     * @param result
     * @param request
     * @return
     */
    @RequestMapping(value = "/getUserVerifies", method = RequestMethod.POST, produces = "application/json")
    public APIResponse getUserVerifies(@RequestBody @Valid ReqUserVerifyQuery userVerifyQuery, BindingResult result, HttpServletRequest request) {
        if (result.hasErrors()) {
            return APIResponse.returnFail(result.getFieldError().getDefaultMessage());
        }
        return APIResponse.returnSuccess(userVerifyService.queryUserVerifyList(userVerifyQuery));
    }

    /**
     * 审核用户
     *
     * @param userAuditParam
     * @return
     */
    @RequestMapping(value = "/verify", method = RequestMethod.POST, produces = "application/json")
    public APIResponse verifyUser(@RequestBody ReqUserAuditParam userAuditParam) {
        try {
            userVerifyService.verifyUser(userAuditParam);
            return APIResponse.returnSuccess();
        } catch (Exception e) {
            logger.error("用户实名审核失败！");
            return APIResponse.returnFail("用户实名审核失败！");
        }
    }

    /**
     * 获取用户列表
     *
     * @param userQuery
     * @param result
     * @param request
     * @return
     */
    @RequestMapping(value = "/getUserList", method = RequestMethod.POST, produces = "application/json")
    public APIResponse getUserList(@RequestBody @Valid ReqUserQuery userQuery, BindingResult result, HttpServletRequest request) {
        if (result.hasErrors()) {
            return APIResponse.returnFail(result.getFieldError().getDefaultMessage());
        }
        return APIResponse.returnSuccess(userService.queryUserList(userQuery));
    }

    /**
     * 获取用户列表
     *
     * @param map
     * @param result
     * @return
     */
    @RequestMapping(value = "/getUserDetail", method = RequestMethod.POST, produces = "application/json")
    public APIResponse getUserDetail(@RequestBody Map<String,Integer> map, BindingResult result) {
        if (result.hasErrors()) {
            return APIResponse.returnFail(result.getFieldError().getDefaultMessage());
        }
        try {
            return APIResponse.returnSuccess(userService.queryUserDetail(map.get("uid")));
        } catch (Exception e) {
            logger.error("获取省份列表失败！", e);
            return APIResponse.returnFail("获取省份列表失败！");
        }
    }

    @RequestMapping(value = "/getProvinceList", method = RequestMethod.POST, produces = "application/json")
    public APIResponse getProvinceList() {
        try {
            return APIResponse.returnSuccess(userService.getProvinceList());
        } catch (Exception e) {
            logger.info("获取省份列表失败！", e);
            return APIResponse.returnFail("获取省份列表失败！");
        }
    }

    @RequestMapping(value = "/getCitys", method = RequestMethod.POST, produces = "application/json")
    public APIResponse getCitys(@RequestBody Map<String, Integer> map, BindingResult result) {
        if (result.hasErrors()) {
            return APIResponse.returnFail(result.getFieldError().getDefaultMessage());
        }

        try {
            return APIResponse.returnSuccess(userService.getCitys(map.get("provinceId")));
        } catch (Exception e) {
            logger.info("获取省份列表失败！", e);
            return APIResponse.returnFail("获取省份列表失败！");
        }
    }

    @RequestMapping(value = "/updateUserGroup", method = RequestMethod.POST, produces = "application/json")
    public APIResponse updateUserGroup(@RequestBody Map<String, Integer> map, BindingResult result) {
        if (result.hasErrors()) {
            return APIResponse.returnFail(result.getFieldError().getDefaultMessage());
        }
        userService.updateUserGroup(map.get("uid"), map.get("groupId"));
        return APIResponse.returnSuccess();
    }

    @RequestMapping(value = "/updateUserInfo", method = RequestMethod.POST, produces = "application/json")
    public APIResponse updateUserInfo(@RequestBody Map<String, Integer> map, BindingResult result) {
        if (result.hasErrors()) {
            return APIResponse.returnFail(result.getFieldError().getDefaultMessage());
        }
        Integer uid = map.get("uid");
        Integer gender = map.get("gender");
        if (uid != null && gender != null){
            ReqUserInfo reqUserInfo = new ReqUserInfo();
            reqUserInfo.setGender(gender);
            try {
                userInformationService.updateUserInfo(reqUserInfo,uid);
            } catch (Exception e) {
                logger.error("更新用户信息失败！");
                return APIResponse.returnFail("更新用户信息失败!");
            }
        }
        return APIResponse.returnSuccess();
    }

    @RequestMapping(value = "/getAdminGroups", method = RequestMethod.POST, produces = "application/json")
    public APIResponse getAdminGroups(@RequestBody Map<String, Integer> map, BindingResult result) {
        if (result.hasErrors()) {
            return APIResponse.returnFail(result.getFieldError().getDefaultMessage());
        }
        return APIResponse.returnSuccess(userService.getAdminGroups());
    }

    @RequestMapping(value = "/insertGroup", method = RequestMethod.POST, produces = "application/json")
    public APIResponse insertGroup(@RequestBody Map<String, String> map, BindingResult result) {
        if (result.hasErrors()) {
            return APIResponse.returnFail(result.getFieldError().getDefaultMessage());
        }
        String groupName = map.get("groupName");
        TbAdminGroup returnAdminGroup = null;
        if (StringUtil.isNotBlank(groupName)){
            TbAdminGroup adminGroup = new TbAdminGroup();
            adminGroup.setGroupName(groupName);
            returnAdminGroup = userService.insertGroup(adminGroup);
        }
        return APIResponse.returnSuccess(returnAdminGroup);
    }

    @RequestMapping(value = "/deleteGroup", method = RequestMethod.POST, produces = "application/json")
    public APIResponse deleteGroup(@RequestBody Map<String, Integer> map, BindingResult result) {
        Integer id = map.get("id");
        int ret = userService.deleteGroup(id);
        if (ret == 1) {
            return APIResponse.returnSuccess();
        }
        return APIResponse.returnFail(result.getFieldError().getDefaultMessage());
    }

    /**
     * 获取用户兴趣列表
     *
     * @param userInterestQuery
     * @param result
     * @param request
     * @return
     */
    @RequestMapping(value = "/getUserInterests", method = RequestMethod.POST, produces = "application/json")
    public APIResponse getUserInterests(@RequestBody @Valid ReqUserInterestQuery userInterestQuery, BindingResult result, HttpServletRequest request) {
        if (userInterestQuery.getUserInterestIds().size() == 0) {
            userInterestQuery.setUserInterestIds(null);
        }
        if (result.hasErrors()) {
            return APIResponse.returnFail(result.getFieldError().getDefaultMessage());
        }
        return APIResponse.returnSuccess(userInterestService.queryUserInterestList(userInterestQuery));
    }

    /**
     * 获取游客兴趣列表
     *
     * @param touristInterestQuery
     * @param result
     * @param request
     * @return
     */
    @RequestMapping(value = "/getTouristInterests", method = RequestMethod.POST, produces = "application/json")
    public APIResponse getTouristInterests(@RequestBody @Valid ReqTouristInterestQuery touristInterestQuery, BindingResult result, HttpServletRequest request) {
        if (touristInterestQuery.getInterestIds().size() == 0) {
            touristInterestQuery.setInterestIds(null);
        }
        if (result.hasErrors()) {
            return APIResponse.returnFail(result.getFieldError().getDefaultMessage());
        }
        return APIResponse.returnSuccess(userInterestService.queryTouristInterestList(touristInterestQuery));
    }


    /**
     * 获取用户投诉列表
     *
     * @param userComplainantQuery
     * @param result
     * @param request
     * @return
     */
    @RequestMapping(value = "/getUserComplainants", method = RequestMethod.POST, produces = "application/json")
    public APIResponse getUserComplainants(@RequestBody @Valid ReqUserComplainantQuery userComplainantQuery, BindingResult result, HttpServletRequest request) {
        if (result.hasErrors()) {
            return APIResponse.returnFail(result.getFieldError().getDefaultMessage());
        }
        return APIResponse.returnSuccess(userComplainantService.queryUserComplainantList(userComplainantQuery));
    }

    /**
     * 获取一口价投诉列表
     *
     * @param reserveComplainantQuery
     * @param result
     * @param request
     * @return
     */
    @RequestMapping(value = "/getReserveComplainants", method = RequestMethod.POST, produces = "application/json")
    public APIResponse getReserveComplainants(@RequestBody @Valid ReqReserveComplainantQuery reserveComplainantQuery, BindingResult result, HttpServletRequest request) {
        if (result.hasErrors()) {
            return APIResponse.returnFail(result.getFieldError().getDefaultMessage());
        }
        return APIResponse.returnSuccess(userComplainantService.queryReserveComplainantList(reserveComplainantQuery));
    }

    /**
     * 获取竞拍投诉列表
     *
     * @param auctionInfoComplainantQuery
     * @param result
     * @param request
     * @return
     */
    @RequestMapping(value = "/getAuctionInfoComplainants", method = RequestMethod.POST, produces = "application/json")
    public APIResponse getAuctionInfoComplainants(@RequestBody @Valid ReqAuctionInfoComplainantQuery auctionInfoComplainantQuery, BindingResult result, HttpServletRequest request) {
        if (result.hasErrors()) {
            return APIResponse.returnFail(result.getFieldError().getDefaultMessage());
        }
        return APIResponse.returnSuccess(userComplainantService.queryAuctionInfoComplainantList(auctionInfoComplainantQuery));
    }

    @RequestMapping(value = "/getComplainantCategory", method = RequestMethod.POST, produces = "application/json")
    public APIResponse getComplainantCategory(@RequestBody Map<String, Integer> map, BindingResult result) {
        if (result.hasErrors()) {
            return APIResponse.returnFail(result.getFieldError().getDefaultMessage());
        }

        try {
            return APIResponse.returnSuccess(userComplainantService.queryComplainantCategoryList(map.get("complainedUid")));
        } catch (Exception e) {
            logger.error("获取用户投诉分类失败！", e);
            return APIResponse.returnFail("获取用户投诉分类失败！");
        }
    }

    /**
     * 用户投诉处理
     *
     * @param disploseComplainant
     * @return
     */
    @RequestMapping(value = "/updateUserStatus", method = RequestMethod.POST, produces = "application/json")
    public APIResponse updateUserStatus(@RequestBody ReqDisploseComplainant disploseComplainant) {
        try {
            userComplainantService.updateUserStatus(disploseComplainant);
            return APIResponse.returnSuccess();
        } catch (Exception e) {
            logger.error("用户投诉处理失败！");
            return APIResponse.returnFail("用户投诉处理失败！");
        }
    }

    @RequestMapping(value = "/getAppVersionList", method = RequestMethod.POST, produces = "application/json")
    public APIResponse getAppVersionList(){
        return APIResponse.returnSuccess(appVersionInfoService.getAppVersionList());
    }

    @RequestMapping(value = "/insertAppVersionInfo", method = RequestMethod.POST, produces = "application/json")
    public APIResponse insertAppVersionInfo(@RequestBody ReqAppVersionInfo appVersionInfo) {
        try {
            Byte appType = appVersionInfo.getAppType() == null ? 0: appVersionInfo.getAppType();
            appVersionInfo.setAppType(appType);
            appVersionInfoService.insertAppVersion(appVersionInfo);
            return APIResponse.returnSuccess();
        } catch (Exception e) {
            logger.error("更新App版本信息失败！");
            return APIResponse.returnFail("更新App版本信息失败！");
        }
    }

    @RequestMapping(value = "/getAppVersionListByOS", method = RequestMethod.POST, produces = "application/json")
    public APIResponse getAppVersionListByOS(@RequestBody Map<String, Integer> map, BindingResult result){
        if (result.hasErrors()) {
            return APIResponse.returnFail(result.getFieldError().getDefaultMessage());
        }

        int os = map.get("os");
        Integer appType = map.get("appType") == null ? 0 : map.get("appType");
        int pageNo = map.get("pageNo");
        int pageSize = map.get("pageSize");
        PageObj<List<RespAppVersionInfo>> listPageObj = appVersionInfoService.getAppVersionListByOS(os,appType,pageNo,pageSize);
        return APIResponse.returnSuccess(listPageObj);
    }

    /**
     * 获取用户账号绑定列表
     *
     * @param userAccountBindQuery
     * @param result
     * @param request
     * @return
     */
    @RequestMapping(value = "/getUserAccountBinds", method = RequestMethod.POST, produces = "application/json")
    public APIResponse getUserAccountBinds(@RequestBody @Valid ReqUserAccountBindQuery userAccountBindQuery, BindingResult result, HttpServletRequest request) {
        if (userAccountBindQuery.getAccountBinds().size() == 0) {
            userAccountBindQuery.setAccountBinds(null);
        }
        if (result.hasErrors()) {
            return APIResponse.returnFail(result.getFieldError().getDefaultMessage());
        }
        return APIResponse.returnSuccess(userAccountBindService.queryUserAccountBindList(userAccountBindQuery));
    }

}
